/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccesoDatos;

/**
 *
 * @author REYES
 */

import java.sql.*;

public class Conexion {
    private Connection conexion = null;
    private String usuario = "root";
    private String password = "root";
    private String localhost = "jdbc:mysql://127.0.0.1/sicgsadb"; // equivalente a localhost

    public Conexion(){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }catch(ClassNotFoundException ex){
            System.out.println(ex.getMessage());
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    
    public Conexion(String usuario, String password, String bd){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.usuario = usuario;
            this.password = password;
            this.localhost = "jdbc:myslq://127.0.0.1/" + bd;
        }catch(ClassNotFoundException ex){
            System.out.println(ex.getMessage());
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    
    public void openConexion(){
        try{
            conexion = DriverManager.getConnection(localhost, usuario, password);
        }catch(SQLException ex){
            getSqlMessage(ex);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }

public void closeConexion(){
        try{
            if(conexion != null){
                conexion.close();
            }
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }
  }
public void getSqlMessage(SQLException ex){
        System.out.println("Sql Error: " + ex.getMessage());
        System.out.println("Sql Estado: " + ex.getSQLState());
        System.out.println("Error: " + ex.getErrorCode());
 }

 public Connection getConexion(){
        return conexion;
 }
     
}
